package jp.co.cos.ittrick.channel.model;

import java.io.Serializable;

import jp.co.cos.ittrick.channel.meta.ChapterModelMeta;

import org.slim3.datastore.Attribute;
import org.slim3.datastore.InverseModelListRef;
import org.slim3.datastore.Model;
import org.slim3.datastore.ModelRef;
import org.slim3.datastore.Sort;
import org.slim3.datastore.json.Json;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.Query.SortDirection;

@Model(schemaVersion = 1)
public class CategoryModel implements Serializable {

    private static final long serialVersionUID = 1L;

    /** キー（INT） */
    @Attribute(primaryKey = true, json=@Json(ignore=true))
    private Key key;

    /** バージョン */
    @Attribute(version = true, json=@Json(ignore=true))
    private Long version;

    /** Category名 */
    private String name;

    /** 表示順 */
    private int index;

    /** ChannelModel に対しての関連 */
    @Attribute(json=@Json(ignore=true))
    private ModelRef<ChannelModel> channelModelRef = new ModelRef<ChannelModel>(ChannelModel.class);

    /** ChapterModelに対しての関連 */
    @Attribute(persistent = false)
    private InverseModelListRef<ChapterModel, CategoryModel> chapterModelListRef =
        new InverseModelListRef<ChapterModel, CategoryModel>(
                ChapterModel.class,
                ChapterModelMeta.get().categoryModelRef.getName(),
                this,
                new Sort(ChapterModelMeta.get().index, SortDirection.ASCENDING));

  //--------------------永久化しない項目---------------------------------

    @Attribute(persistent = false)
    private String keyValueString;

    public long getKeyString() {
        return key.getId();
    }

    public String getKeyValueString() {
        return String.valueOf(key.getId());
    }

//----------------------メソッド---------------------------------------

    /**
     * Returns the key.
     *
     * @return the key
     */
    public Key getKey() {
        return key;
    }

    /**
     * Sets the key.
     *
     * @param key
     *            the key
     */
    public void setKey(Key key) {
        this.key = key;
    }

    /**
     * Returns the version.
     *
     * @return the version
     */
    public Long getVersion() {
        return version;
    }

    /**
     * Sets the version.
     *
     * @param version
     *            the version
     */
    public void setVersion(Long version) {
        this.version = version;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((key == null) ? 0 : key.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        CategoryModel other = (CategoryModel) obj;
        if (key == null) {
            if (other.key != null) {
                return false;
            }
        } else if (!key.equals(other.key)) {
            return false;
        }
        return true;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public int getIndex() {
        return index;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public ModelRef<ChannelModel> getChannelModelRef() {
        return channelModelRef;
    }

    public InverseModelListRef<ChapterModel, CategoryModel> getChapterModelListRef() {
        return chapterModelListRef;
    }
}
